package com.cheyun.netsalev3.view.dm

import android.os.Bundle
import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.dm.DmClueList
import com.cheyun.netsalev3.databinding.FragmentDmXktListBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.DmXktViewModel

class DmXktListFragment : BaseFragment<FragmentDmXktListBinding>() {
    private var planType = 1
    private var dealed = 1
    private var status = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            planType = it.getInt("planType")
            dealed = it.getInt("dealed")
            status = it.getInt("status")
        }
    }

    override fun initContentView(): Int {
        return R.layout.fragment_dm_xkt_list
    }

    override fun initViewModel(): ViewModel? {
        activity?.apply {
            return ViewModelProviders.of(this).get(DmXktViewModel::class.java)
        }
        return null
    }

    private lateinit var adapter: MyPagedListAdapter<DmClueList>

    override fun initView() {
        adapter = MyPagedListAdapter(R.layout.fragment_dm_xkt_item, binding.viewModel)
        var recyclerView = binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.adapter = adapter
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh(status)
        }
    }

    override fun initData() {
        binding.viewModel!!.pagedlist[status]?.observe(this, Observer { list ->
            Log.e("pagedlist....", list.toString())
            list?.apply {
                adapter?.submitList(this)
            }
        })
        binding.viewModel!!.hashMap[status]?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
            adapter?.setNetworkState(it)
        })
    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")
    }

    companion object {
        @JvmStatic
        fun newInstance(planType: Int,dealed:Int ,status:Int) =
                DmXktListFragment().apply {
                    arguments = Bundle().apply {
                        putInt("planType", planType)
                        putInt("dealed", dealed)
                        putInt("status", status)
                    }
                }
    }


}
